$(function(){
  	createTree();  
});

/**
 * 创建菜单树
 */
function createTree(){	
   $("#tree").tree({
      url:'department.do?method=queryDepartment&tid=-1',
      onBeforeExpand:function(node,param){   
         $('#tree').tree('options').url = "department.do?method=queryDepartment&tid="+node.id;
      },
      lines : true,      
      onContextMenu:function(e,node){
         e.preventDefault();
         $('#tree').tree('select', node.target);
         $('#mm').menu('show', {
			left: e.pageX,
			top: e.pageY,
			onClick:function(item){
			   var pid = node.id;
			   var pcode = node.code;
			   var pname = node.text;
			   var id = item.id;
			   var orgNature = node.orgNature;
			   
			   if(id=='add_btn'){
				   //根据节点是否为公司判断能否添加子节点
				   if(orgNature != 10){
					   $.messager.alert("系统提示","只能在公司节点添加组织机构！","warning");
				   }else {
					   addDepartment(pid,pname,pcode);
				   }
			   }else if(id=='update_btn'){
				   updateDepartment(node.pid,node.parentOrgName,pid,pname,pcode,node.depth,node.state,node.managerId,node.orgNature,node.orgMemo);
			   }else if(id=='delete_btn'){
				   deleteDepart(pid);
			   }			   
			}
		 });
      }
   });
}
function addDepartment(pid,pname,pcode){
   $("#addWin").window('setTitle',"添加组织机构").window('open');
   //备注栏高度orgMemo
   $("#orgMemo").attr('style','border:0px;width:99%;height:50px;overflow-y:hidden;');
   
   $("#saveForm").form('clear');
   $("#saveForm").form('load',{
      pid:pid,pcode:pcode,pname:pname,orgId:'',orgCode:'',seqnum:'1'
   });
}
function updateDepartment(pid,parentOrgName,id,name,code,seqnum,dleaf,managerId,orgNature,orgMemo){
   $("#updateWin").window('setTitle',"修改组织机构").window('open');
   //备注栏高度orgMemo
   $("#orgMemoUpdate").attr('style','border:0px;width:99%;height:50px;overflow-y:hidden;');
//   $("#saveForm").form('clear');
   $("#updateForm").form('load',{
	   pnameUpdate:name,orgIdUpdate:id,orgCodeUpdate:code,orgNameUpdate:name,seqnumUpdate:seqnum
	   ,dleafUpdate:dleaf,managerIdUpdate:managerId,orgNatureUpdate:orgNature,orgMemoUpdate:orgMemo
   });
 
   //上级组织机构树状结构
   initCity(pid,parentOrgName);

}
$("#add_save").click(function(){
   var orgId = $("#orgId").val();
   if(isEmpty(orgId)){
      submitForm({
       formId:'saveForm',
       url:'department.do?method=addDepartment',
       callback:callback_add
     });
   }else{
       submitForm({
        formId:'saveForm',
        url:'department.do?method=updateDepartment',
        callback:callback_update
      });   
   }
});

$("#update_save").click(function(){
   submitForm({
    formId:'updateForm',
    url:'department.do?method=updateDepartment',
    callback:callback_update
  });   
});
//保存菜单后的回调方法
function callback_add(data){    
   if(data=="SUCCESS"){   	   	  
   	  $.messager.confirm("系统提示","组织机构信息增加成功，是否继续添加？",reloadData);
   }else if(data=="FAIL"){   	  
   	  $.messager.alert("系统提示","组织机构信息增加失败，请重试！","error");
   }else{
   	  $.messager.alert("系统提示","添加组织机构时系统异常，请联系系统维护人员！","error");	
   }
}
//保存菜单后的回调方法
function callback_update(data){    
   if(data=="SUCCESS"){   	   	  
   	  $.messager.alert("系统提示","组织机构信息修改成功！","info",reload1);
   }else if(data=="FAIL"){   	  
   	  $.messager.alert("系统提示","组织机构信息修改失败，请重试！","error");
   }else{
   	  $.messager.alert("系统提示","修改组织机构时系统异常，请联系系统维护人员！","error");	
   }
}
//刷新数据
function reloadData(r) {	
	if(r){		
	    clearValue();
	}else{	   	   
	   $('#addWin').window('close');
	}
	reloadTree1();
}
function reload1(){	
	$('#updateWin').window('close');
	reloadTree({treeId:'tree'});
}
function clearValue(){
   $("#orgName").val(""); 
   $("#seqnum").val("");
   $("#orgMemo").val("");
}
function reloadTree1(){
  try{
    reloadTree({treeId:'tree'});
  }catch(e){alert(e);}
}
function deleteDepart(id){
    var url = "department.do?method=deleteDepartment";
     //删除数据
   deleteData({
     len:1,
     url:url,
     params:{'id':id,'version':Math.random()},
     dataType:'text',
     callback:function(msg){
        if(msg=="EMPTY"){
           $.messager.alert("系统提示","请选择要删除的记录","warning");
        }else if(msg=="FAIL"){
           $.messager.alert("系统提示","删除组织机构信息失败","warning");
        }else if(msg=="SUCCESS"){          
		   reloadTree({treeId:'tree'}); 
        }else{
            $.messager.alert("系统提示","系统出现异常："+msg,"error");
        }
     }
   });
}


function initCity(pid,pName){
   $("#pnameUpdate").combotree({
   	  width:270,
      url:'department.do?method=queryDepartment&tid=-1',
      onBeforeExpand:function(node,param){   
//         $('#pname').combotree('tree').tree('options').url = "department.do?method=queryDepartment&type=user&tid="+node.id;
	    $('#pnameUpdate').combotree('tree').tree('options').url = "department.do?method=queryDepartment&tid="+node.id; 
   	},
      onLoadSuccess:function(node,data){
//    	  url:'department.do?method=queryDepartment&tid='+node.id,
   		$("#pidUpdate").val(pid);
        $('#pnameUpdate').combotree('setText', pName);
      },
   	onSelect:function(node){
    	  $("#pidUpdate").val(node.id);
      }
   });
}
